package demo3;

public class MySingleList {
  static  class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(6);
        ListNode node2 = new ListNode(6);
        ListNode node3 = new ListNode(6);
        node1.next = node2;
        node2.next = node3;
        this.head = node1;
    }

    public void size(){

    }

    public void show(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + "   ");
            cur =cur.next;
        }
        System.out.println();
    }

    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        while (head != null && head.val == val ){
            head = head.next;
        }
        ListNode cur = head;
        ListNode prev = head;
        while(cur != null){
            if(cur.val == val){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        return head;

    }


}
